home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 127 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.9 KB  |  84 lines

  1. Newsgroups: comp.std.c++
  2. Path: cs.mu.OZ.AU!bounce-back
  3. From: rsm@gateway.Dtseng.Com (Roger S. Morris)
  4. Subject: Re: STL still in standard
  5. Message-ID: <DLnwF8.MF2@mv.mv.com>
  6. Apparently-To: comp-std-c++@uunet.mv.com
  7. Originator: fjh@munta.cs.mu.OZ.AU
  8. Sender: news@cs.mu.OZ.AU (CS-Usenet)
  9. Organization: Discrete Time Systems Corporation
  10. References: <4dgrb4$a2e@engnews1.Eng.Sun.COM> <4dd7on$djk@rc1.vub.ac.be> <199601191512.IAA09192@tijeras.scd.ucar.edu>
  11. X-Original-Date: Wed, 24 Jan 1996 01: 35:31 GMT
  12. Date: Wed, 24 Jan 1996 01:40:28 GMT
  13. Approved: fjh@cs.mu.oz.au
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBFAgUBMQWON+EDnX0m9pzZAQF5nwF9Fd2B6yZZ9Eh92sCnKVTckV+NxkvAvI8m
  16.     SZfGGsbMbJ/qrP0HvFXfogu3b7wDBuEY
  17.     =/k3y
  18.  
  19. Related to the apparent "un-object-orientedness" of STL, one problem we've
  20. run into is the inability to gracefully store abstract objects in a container.
  21.  
  22. One solution is to wrap the object to be stored in some sort of handle.
  23. For those who like to wrap everything in a "handle", this works well.  For
  24. others, this is quite an inconvenience.
  25.  
  26. Another solution is to use a container of pointers-to-T.  This, however,
  27. makes for some ugly and dangerous code.
  28.  
  29. Our solution was to create the templates ``plist'', ``pvector'',
  30. ``pset'', etc.  These are containers that meet the requirements of
  31. ``list'', ``vector'', etc., but internally use pointers instead of
  32. actual copies of the objects.
  33.  
  34. We've extended the role of the allocator to include ``T* make_default()'' and 
  35. ``T* make_copy(const T&)'' functions.  This way, a ``plist<T>'' (for example)
  36. can meet all the requirements of a ``list<T>'', even if T lacks an actual
  37. default ctor or a copy ctor.
  38.  
  39. Often, We've found that make_copy is defined as ``{ return t.replicate(); }'',
  40. where ``T* replicate()'' is a virtual function for duplicating and returning a
  41. pointer to the new copy.  We do this so often that, in our plist.h, we
  42. have a:
  43.    template<class T> class plistB<T> : public plist<T,p_allocatorB<T> > {...};
  44. where ``p_allocatorB<T>'' assumes T has a ``replicate'' member function, and
  45. that T cannot be default constructed.  Note that, due to lack of a default
  46. constructor, plistB<T> can't do everything that list<T> can, but this is
  47. usually not a problem.
  48.  
  49. Also, we have an p_allocatorA<T> which simply does a ``{ return new T(t); }''
  50. for make_copy(), and a ``{ return new T; }'' for make_default().  Our
  51. ``plistA<T>'' uses this allocator (our compiler doesn't support default
  52. template args).
  53.  
  54.  
  55. We are currently adding some special iterator tags to determine if the
  56. iterator is to one of our "p*" containers.  Using these tags, we should
  57. be able to speed up those algorithms that move elements around within or
  58. between containers (since moving a pointer is almost always faster than
  59. moving an object).  The "p*" container iterators offer the following
  60. additional functions:
  61.     T* get_p();                 // These should always be used together
  62.     void set_p(T*);             //  "
  63. and the "plist" (for example) offers:
  64.     void push_back_p( T* p );                     // ``p'' is deletable
  65.     void push_front_p( T* p );                    //  "
  66.     iterator insert_p(iterator position, T* p );  //  "
  67.     T* pop_back_p();                // Caller must worry about deleting 
  68.     T* pop_front_p();               //  returned value.
  69.     T* erase_p(iterator position);  //  "
  70. All of the above (slightly dangerous) extensions transfer "ownership" of
  71. a deletable pointer between caller and callee.  These extensions are
  72. primarily for use by well-tested algorithms.  The general user would
  73. typically stick to the basic set of (safe) STL-defined routines.
  74.  
  75.  
  76. Maybe by using something like this, the original poster would find
  77. STL more OO-friendly.
  78.  
  79. --Roger Morris
  80. ---
  81. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  82.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  83.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  84.